package com.lincoln.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Lincoln
 */
@Configuration
@EnableWebSecurity
public class WebMvcConfiguration implements WebMvcConfigurer {
    public static Logger logger = LoggerFactory.getLogger(WebMvcConfiguration.class);
    @Autowired
    MyHandlerInterceptor myHandlerInterceptor;

    @Value("${picture.win-src}")
    String winSrc;

    @Value("${picture.win-other}")
    String winOther;

    @Value("${picture.linux-src}")
    String linuxSrc;

    @Value("${picture.linux-other}")
    String linuxOther;

//	@Override
//	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
//		argumentResolvers.add(currentUserHandlerMethodArgumentResolver());
//	}

//	@Bean
//	public HandlerMethodArgumentResolver currentUserHandlerMethodArgumentResolver() {
//		return new HandlerMethodArgumentResolver() {
//			@Override
//			public boolean supportsParameter(MethodParameter parameter) {
//				return parameter.getParameterType().equals(CustomPrincipal.class);
//			}
//
//			@Override
//			public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
//                                          NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
//				try {
//					return (CustomPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//				} catch (Exception e) {
//					return null;
//				}
//			}
//		};
//	}

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myHandlerInterceptor).addPathPatterns("/**");
    }

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        boolean isWindows = System.getProperty("os.name").toLowerCase().indexOf("windows") != -1;
        if (isWindows) {
            registry.addResourceHandler("/picture/src/**").addResourceLocations("file:" + winSrc + "\\");
            registry.addResourceHandler("/picture/**").addResourceLocations("file:" + winOther + "\\");
        } else {
            registry.addResourceHandler("/picture/src/**").addResourceLocations("file:" + linuxSrc + "\\");
            registry.addResourceHandler("/picture/**").addResourceLocations("file:" + linuxOther + "\\");
        }
    }
}